let baseUrl = 'https://api.77du.net/ciff/miniappapi' //环境一
let baseUrl2 = 'https://api.77du.net/ciff' //微信登录的地址接口
// let app=getApp()
// console.log(app)
//带Token请求
export const http = (opts, data, whitelist = false) => {
  let token = uni.getStorageSync('userInfo').token || '' //用户令牌
  // if (!whitelist) {
  //   if (!token) {
  //     uni.showToast({
  //       title: '还未登录',
  //       icon: 'none',
  //       duration: 1200,
  //     })
  //     // setTimeout(() => {
  //     //   uni.switchTab({
  //     //     url: '/pages/my/my',
  //     //   })
  //     // }, 1200)
  //     // this.authSwitch=true
  //     // app.offAuth=true
  //     wx.$AuthOpen()
  //     return
  //   }
  // }
  let getheader = {
    token: token,
    'X-Requested-With': 'XMLHttpRequest',
    Accept: 'application/json',
    'Content-Type': 'application/json; charset=UTF-8',
    r: new Date().getTime(),
    apiVersion: 1,
  }
  let otherHeader = {
    token: token,
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    r: new Date().getTime(),
    apiVersion: 1,
  }
  //是否需要带上uuid

  let httpDefaultOpts = {
    url: (opts.urlType == true ? baseUrl2 : baseUrl) + opts.url,
    data: data,
    method: opts.method,
    header: opts.method == 'get' ? getheader : otherHeader,
    dataType: 'json',
  }
  let promise = new Promise(function (resolve, reject) {
    uni
      .request(httpDefaultOpts)
      .then((res) => {
        resolve(res)
      })
      .catch((response) => {
        reject(response)
        uni.showToast({
          title: '网络错误',
          duration: 1000,
          mask: true,
          position: true,
        })
      })
  })
  return promise
}
